package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.sumpu.app.guoguojie.dao.MemberDao;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.MemberAudit;
import cn.sumpu.app.guoguojie.domain.Ext.FollowFans;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class MemberDaoImpl extends SqlSessionDaoSupport implements MemberDao {

    private static final Logger logger = LoggerFactory.getLogger(MemberDaoImpl.class);

    public void insert(Member member) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.MemberMapper.insert", member);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Write database failure.", e);
            }
            throw new AppBusinessException("Write database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    public List<Member> getMemberList(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Query database failure.", e);
            }
            throw new AppBusinessException("Query database failure.", e);
        }
    }

    @Override
    public int getMemberListCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberListCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Query getMemberListCount failure.", e);
            }
            throw new AppBusinessException("Query getMemberListCount failure.", e);
        }
    }

    @Override
    public void updateMember(Member member) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.MemberMapper.updateMember", member);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Update member failure.", e);
            }
            throw new AppBusinessException("Update member failure.", e);
        }
    }

    @Override
    public void updateHostName(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.MemberMapper.updateHostName", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateHostName failure.", e);
            }
            throw new AppBusinessException("updateHostName failure.", e);
        }
    }

    @Override
    public Member getMemberById(String identifier) throws AppBusinessException {

        try {
            return (Member) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberById", identifier);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMemberById failure.", e);
            }
            throw new AppBusinessException("getMemberById failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Member> getMemberListByDoorplate(String doorplate) throws AppBusinessException {
        try {
            return (List<Member>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberListByDoorplate", doorplate);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMemberListByDoorplate failure.", e);
            }
            throw new AppBusinessException("getMemberListByDoorplate failure.", e);
        }
    }

    // 根据标识号删除宝宝信息
    @Override
    public void deleteBabyInfo(String identifier) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MemberMapper.deleteBabyInfo", identifier);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteBabyInfo failure.", e);
            }
            throw new AppBusinessException("deleteBabyInfo failure.", e);
        }
    }

    /**
     * 根据门牌号取得爸爸妈妈的集合
     * 
     * @param doorplate
     *            门牌号
     * @throws AppBusinessException
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Member> getParentListByDoorplate(String doorplate) throws AppBusinessException {
        try {
            return (List<Member>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getParentListByDoorplate", doorplate);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getParentListByDoorplate failure.", e);
            }
            throw new AppBusinessException("getParentListByDoorplate failure.", e);
        }
    }

    @Override
    public String getHostPwdByDoorplate(String doorplate) throws AppBusinessException {
        try {
            return (String) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getHostPwdByDoorplate", doorplate);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getHostPwdByDoorplate failure.", e);
            }
            throw new AppBusinessException("getHostPwdByDoorplate failure.", e);
        }
    }

    @Override
    public Member getHostInfoByDoorplate(String doorplate) throws AppBusinessException {
        try {
            return (Member) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getHostInfoByDoorplate", doorplate);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getHostInfoByDoorplate failure.", e);
            }
            throw new AppBusinessException("getHostInfoByDoorplate failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Member> getMemberListByIds(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<Member>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberListByIds", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMemberListByIds database failure.", e);
            }
            throw new AppBusinessException("getMemberListByIds database failure.", e);
        }
    }

    @Override
    public int getMemberListByIdsCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberListByIdsCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMemberListByIdsCount database failure.", e);
            }
            throw new AppBusinessException("getMemberListByIdsCount database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<MemberAudit> getMemberAuditList(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<MemberAudit>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberAuditList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMemberAuditList failure.", e);
            }
            throw new AppBusinessException("getMemberAuditList failure.", e);
        }
    }

    @Override
    public int deleteMemberAuditByIds(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MemberMapper.deleteMemberAuditByIds", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteMemberAuditByIds failure.", e);
            }
            throw new AppBusinessException("deleteMemberAuditByIds failure.", e);
        }
    }

    @Override
    public int getFamilyListByMemberNameCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.DoingMapper.getFamilyListByMemberNameCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getFamilyListByMemberNameCount database failure.", e);
            }
            throw new AppBusinessException("getFamilyListByMemberNameCount database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<FollowFans> getFamilyListByMemberName(Map<String, Object> map) throws AppBusinessException {
        try {
            return (List<FollowFans>) getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.DoingMapper.getFamilyListByMemberName", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getFamilyListByMemberName database failure.", e);
            }
            throw new AppBusinessException("getFamilyListByMemberName database failure.", e);
        }
    }

    @Override
    public int getFamilyBabyCount(String family) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getFamilyBabyCount", family);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getFamilyBabyCount database failure.", e);
            }
            throw new AppBusinessException("getFamilyBabyCount database failure.", e);
        }
    }

    // 根据昵称nickname取得会员信息
    @Override
    public Member getMemberByNickname(String nickname) throws AppBusinessException {
        try {
            return (Member) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberByNickname", nickname);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMemberByNickname database failure.", e);
            }
            throw new AppBusinessException("getMemberByNickname database failure.", e);
        }
    }

    // 取得全部的会员昵称和门牌号
    @SuppressWarnings("unchecked")
    @Override
    public List<Member> getAllMember(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getAllMember", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getAllMember database failure.", e);
            }
            throw new AppBusinessException("getAllMember database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    public List<Member> getUserInfoList(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getUserInfoList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getUserInfoList database failure.", e);
            }
            throw new AppBusinessException("getUserInfoList database failure.", e);
        }
    }

    @Override
    public int getUserInfoListCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getUserInfoListCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getUserInfoListCount database failure.", e);
            }
            throw new AppBusinessException("getUserInfoListCount database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Member> getApproveMemberList(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getApproveMemberList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getApproveMemberList database failure.", e);
            }
            throw new AppBusinessException("getApproveMemberList database failure.", e);
        }
    }

    @Override
    public int getApproveMemberListCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getApproveMemberListCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getApproveMemberListCount database failure.", e);
            }
            throw new AppBusinessException("getApproveMemberListCount database failure.", e);
        }
    }

    //超级帐号

    @Override
    public int getSuperUserListCount(Map<String, Object> map) throws AppBusinessException {
        try {
            return (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getSuperUserListCount", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getApproveMemberListCount database failure.", e);
            }
            throw new AppBusinessException("getApproveMemberListCount database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Member> getSuperUserList(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getSuperUserList", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getApproveMemberList database failure.", e);
            }
            throw new AppBusinessException("getApproveMemberList database failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Member> getMember(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getMember", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMember failure.", e);
            }
            throw new AppBusinessException("getMember  failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Member> getLoginMember(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getLoginMember", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMember failure.", e);
            }
            throw new AppBusinessException("getMember  failure.", e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<FollowFans> getFans(Map<String, Object> map) throws AppBusinessException {
        try {
            return getSqlSession().selectList("cn.sumpu.app.guoguojie.persistence.MemberMapper.getFans", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getFans failure.", e);
            }
            throw new AppBusinessException("getFans  failure.", e);
        }
    }

    @Override
    public int getFansCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getFansCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getFansCount  failure.", e);
            }
            throw new AppBusinessException("getFansCount failure.", e);
        }
    }

    @Override
    public void insertFans(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.MemberMapper.insertFans", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("insertFans failure.", e);
            }
            throw new AppBusinessException("insertFans failure.", e);
        }
    }

    @Override
    public void deleteFans(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MemberMapper.deleteFans", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("deleteFans failure.", e);
            }
            throw new AppBusinessException("deleteFans failure.", e);
        }
    }

    /**
     * 只用来做数据订正.
     * 
     * @param map
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.dao.MemberDao#updateFollowFans(java.util.Map)
     */
    @Override
    public void updateFollowFans(Map<String, Object> map) throws AppBusinessException {
        try {
            getSqlSession().update("cn.sumpu.app.guoguojie.persistence.MemberMapper.updateFans", map);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateFans failure.", e);
            }
            throw new AppBusinessException("updateFans failure.", e);
        }
    }

    @Override
    public int getMemberCount(Map<String, Object> map) throws AppBusinessException {
        try {
            Object obj = (Integer) getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MemberMapper.getMemberCount", map);
            return (Integer) (obj == null ? 0 : obj);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("getMemberCount database failure.", e);
            }
            throw new AppBusinessException("getMemberCount database failure.", e);
        }
    }
}
